# _*_ coding : utf-8 _*_
# @Time: 2024/9/23 14:02
# @Author : 石文帅	
# @File : log.py
# @Project : python_slfh

import time

import os
import logging


# 运行后，在控制台和日志文件中会看到设置的日志信息
from common.getini import get_db_data


class log_site:
    def __init__(self):
        # 创建logger
        self.logger = logging.getLogger('my_logger')
        self.logger.setLevel(logging.INFO)  # 设置日志级别

        # 获取当前工作目录
        current_directory = os.getcwd()

        # 前一个目录路径
        previous_path = get_db_data('pageboys', 'previous_path')

        current_time = time.strftime('%Y%m%d%H%M%S', time.localtime())
        # 创建 file handler，输出到日志文件
        fh = logging.FileHandler(previous_path + f'/log/{current_time}.log')
        fh.setLevel(logging.INFO)

        # 创建stream handler，输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

    def info(self, msg, *args, **kwargs):
        self.logger.info(msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        self.logger.error(msg, *args, **kwargs)


if __name__ == '__main__':
    log = log_site()
    log.info('123')